package com.gsq.learning.mybatisplugin.plugin.interceptor;

import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Properties;

@Intercepts(
        {
                @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}),
                @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}),
        }
)
public class MyPageInterceptor implements Interceptor {

    private Integer pageNo;
    private Integer pageSize;

    @Override
    public Object intercept(Invocation invocation) throws Throwable {

        Executor target = (Executor) invocation.getTarget();
        System.out.println("======> MyPageInterceptor：" + target);


        Object[] args = invocation.getArgs();
        MappedStatement ms = (MappedStatement) args[0];
        Object parameter = args[1];
        RowBounds rowBounds = (RowBounds) args[2];
        ResultHandler resultHandler = (ResultHandler) args[3];
        Executor executor = (Executor) invocation.getTarget();
        CacheKey cacheKey;
        BoundSql boundSql;
        //由于逻辑关系，只会进入一次
        if(args.length == 4){
            //4 个参数时
            boundSql = ms.getBoundSql(parameter);
            cacheKey = executor.createCacheKey(ms, parameter, rowBounds, boundSql);
        } else {
            //6 个参数时
            cacheKey = (CacheKey) args[4];
            boundSql = (BoundSql) args[5];
        }

        Class<BoundSql> boundSqlClass = BoundSql.class;
        Field sqlField = boundSqlClass.getDeclaredField("sql");
        sqlField.setAccessible(true);
        String o = (String) sqlField.get(boundSql);
        System.out.println("o = " + o);

        int offset = pageNo * pageSize;
        if (!o.contains("limit")) {
            o += " limit " + offset + "," + pageSize;
        }
        sqlField.set(boundSql, o);
        System.out.println("o = " + o);



//        Object proceed = invocation.proceed();
//        Object proceed1 = invocation.proceed();

        return executor.query(ms, parameter, rowBounds, resultHandler, cacheKey, boundSql);
    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {
        pageNo = Integer.parseInt(properties.getProperty("pageNo"));
        pageSize = Integer.parseInt(properties.getProperty("pageSize"));
    }
}
